import { EnergyMgr } from "../../resources/components/energy/EnergyMgr";
import { EventKey } from "../common/Key";
import { GameData } from "../game/GameData";
import { GameState } from "../game/GameState";
var TurntableData = [
    [0, 10, 1, 4],// 金币
    [1, 7, 0, 0],
    [2, 4, 1, 8],
    [3, 7, 0, 0],
    [4, 6, 2, 50],// 钻石
    [5, 7, 0, 0],
    [6, 3, 3, 5], // 能量
    [7, 7, 0, 0],
]
cc.Class({
    extends: require('BasePanel'),

    properties: {
        wheelSp: {
            default: null,
            type: cc.Node
        },
        maxSpeed: {
            default: 5,
            type: cc.Float,
            max: 15,
            min: 2,
        },
        duration: {
            default: 3,
            type: cc.Float,
            max: 5,
            min: 1,
            tooltip: "减速前旋转时间"
        },
        acc: {
            default: 0.1,
            type: cc.Float,
            max: 0.2,
            min: 0.01,
            tooltip: "加速度"
        },
        
        sprEnergy: cc.Sprite,
        labEnergy: cc.Label,
    },
    onLoad() {
        this._super();
    },

    _updateEnergyUI() {
        let energyStr = EnergyMgr.getFormatEnergy();
        this.labEnergy.string = energyStr;

        let energyScale = EnergyMgr.getEnergyScale();
        this.sprEnergy.fillRange = energyScale;
    },

    onEnter(args, action = true) {
        this._super(args, action);

        this.wheelState = 0;
        this.curSpeed = 0;
        this.spinTime = 0;                   //减速前旋转时间
        this.gearNum = 8;
        this.defaultAngle = 360 / this.gearNum / 2;        //修正默认角度
        this.gearAngle = 360 / this.gearNum;   //每个齿轮的角度
        this.wheelSp.angle = this.defaultAngle;
        this.finalAngle = 0;                 //最终结果指定的角度

        this._updateEnergyUI();
    },

    onExit(args) {
        this._super(args);


    },

    startRotate() {
        if (this.wheelState !== 0) {
            return;
        }
        G.partner.sendEvent('转盘', { '类型': '转盘' });
        G.game.toast('能量-1',0.5);
        this.targetID = G.utils.randomWeight(TurntableData);

        this.decAngle = 2 * 360;  // 减速旋转两圈
        this.wheelState = 1;
        this.curSpeed = 0;
        this.spinTime = 0;

        this.randomRo = G.utils.random(-this.gearAngle / 2 * 0.9, this.gearAngle / 2 * 0.9);

        EnergyMgr.subEnergy();
    },

    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        if (this.wheelState === 0) {
            return;
        }
        // cc.log('......update');
        // cc.log('......state=%d',this.wheelState);

        if (this.wheelState == 1) {
            // cc.log('....加速,speed:' + this.curSpeed);
            this.spinTime += dt;
            this.wheelSp.angle = this.wheelSp.angle - this.curSpeed;
            if (this.curSpeed <= this.maxSpeed) {
                this.curSpeed += this.acc;
            } else {
                if (this.spinTime < this.duration) {
                    return;
                }
                // cc.log('....开始减速');
                //设置目标角度
                this.finalAngle = 360 - this.targetID * this.gearAngle - this.defaultAngle;
                this.maxSpeed = this.curSpeed;
                this.wheelSp.angle = -this.finalAngle;
                this.wheelState = 2;
            }
        } else if (this.wheelState == 2) {
            // cc.log('......减速');
            var curRo = - this.wheelSp.angle; //应该等于finalAngle
            var hadRo = curRo - this.finalAngle;
            this.curSpeed = this.maxSpeed * ((this.decAngle - hadRo + this.randomRo) / this.decAngle) + 0.2;
            this.wheelSp.angle = -curRo - this.curSpeed;

            if ((this.decAngle - hadRo + this.randomRo) <= 0) {
                // cc.log('....停止');
                this.wheelState = 0;
                // this.wheelSp.rotation = this.finalAngle;
                this.showRes();
            }
        }
    },
    showRes: function () {
        let data = TurntableData[this.targetID];
        let type = data[2];
        let count = data[3];
        if (type == 0) {
            let toast = ['相信我，下一把必中', '搏一搏，摩托没有了', '哎呀，只差一点点', '吹一口仙气，中奖几率更大哦'];
            G.game.toast(toast[G.utils.random(0, toast.length - 1)], 1);
        } else if (type == 1) {
            // 金币
            let animalIndex = GameState.getMaxIndexCanBuyByCoin();
            let price = GameState.getAnimalCoinPrice(animalIndex);
            price = G.bignumber.mult(price, count);
            GameData.addCoin(price);
            console.log(animalIndex,price)
            G.game.toast('金币+' + G.bignumber.toUnitType(price), 1);
        } else if (type == 2) {
            // 钻石
            GameData.addDiamond(count);
            G.game.toast('钻石+' + count, 1);
        } else if (type == 3) {
            // 能量
            EnergyMgr.addEnergys(count);
            G.game.toast('能量+' + count, 1);
        }
    },



    show() {
        this._super();

    },

    hide(cb) {
        this._super(cb);
    },
    onEnable() {
        this._registEvent();
    },
    onDisable() {
        this._unRegistEvent();
    },

    buttonListener: function (button) {
        G.game.gameAudioMgr.playBtn();
        let name = button.node.name;
        if (name == 'btnClose') {
            if (this.wheelState !== 0) {
                return;
            }
            this.onClose();
        } else if (name == 'btnStartRotate') {
            this.startRotate();
        }
    },

    _registEvent() {
        G.onFire.on(EventKey.EVENT_ENERGY_CHANGE, this._onFire, this);

    },
    _unRegistEvent() {
        G.onFire.off(EventKey.EVENT_ENERGY_CHANGE, this._onFire, this);
    },

    _onFire(eventName, param) {
        if (eventName == EventKey.EVENT_ENERGY_CHANGE) {
            this._updateEnergyUI();
        }
    },

    onTouchStart() {

    },
});
